﻿Ext.LoadMask = Ext.extend(Ext.util.Observable, {



    msg: 'Loading...',

    msgCls: 'x-mask-loading',


    disabled: false,

    constructor: function (el, config) {
        this.el = Ext.get(el);
        Ext.apply(this, config);

        this.addEvents('show', 'hide');

        //如果有数据存储，则绑定数据
        if (this.store) {
            this.bindStore(this.store, true);
        }

        Ext.LoadMask.superclass.constructor.call(this);
    },

    //Changes the data store bound to this LoadMask.
    bindStore: function (store, initial) {
        if (!initial && this.store) {
            this.mun(this.store, {
                scope: this,
                beforeload: this.onBeforeLoad,//数据加载前的响应函数
                load: this.onLoad,//数据加载的时候触发的响应函数
                exception: this.onLoad
            });
            if (!store) {
                this.store = null;
            }
        }
        //如果参数有数据存储传来
        if (store) {
            //获得实际数据存储实例，并注册侦听函数
            store = Ext.StoreMgr.lookup(store);
            this.mon(store, {
                scope: this,
                beforeload: this.onBeforeLoad,
                load: this.onLoad,
                exception: this.onLoad
            });

        }

        this.store = store;//赋给this.store

        //如果store正在加载数据
        if (store && store.isLoading()) {
            this.onBeforeLoad();//触发函数
        }
    },


    disable: function () {
        this.disabled = true;
    },


    enable: function () {
        this.disabled = false;
    },


    isDisabled: function () {
        return this.disabled;
    },


    onLoad: function () {
        this.el.unmask();
        this.fireEvent('hide', this, this.el, this.store);
    },

 
    onBeforeLoad: function () {
        if (!this.disabled) {
            this.el.mask(Ext.LoadingSpinner + '<div class="x-loading-msg">' + this.msg + '</div>', this.msgCls, false);
            this.fireEvent('show', this, this.el, this.store);
        }
    },


    show: function () {
        this.onBeforeLoad();
    },


    hide: function () {
        this.onLoad();
    },


    destroy: function () {
        this.hide();
        this.clearListeners();
    }
});